/*
* Copyright (C) 1999.4  Li ZhenChun
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License; or
* (at your option) any later version.
*
* This program is distributed in the hope that is will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of 
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, M A 02139, USA.
*
* Author: Li ZhenChun  email: zhchli@163.net or zhchli@126.com
* 
*/
#include "stdafx.h"
#include "freepy.h"

BOOL RepeatPreResult(HIMC hIMC)
{
	LPINPUTCONTEXT lpIMC;
	LPCOMPOSITIONSTRING lpCompStr;
	LPTSTR lpPreResultStr,lpConvStr;

	wConversionMode = 0;

	lpIMC = ImmLockIMC(hIMC);
	lpCompStr = (LPCOMPOSITIONSTRING)ImmLockIMCC(lpIMC->hCompStr);

	lpPreResultStr = ((LPMYCOMPSTR)lpCompStr)->FreePYComp.szPreResultStr;
	if( _tcslen(lpPreResultStr) )
	{
		lpConvStr = ((LPMYCOMPSTR)lpCompStr)->FreePYComp.szConvCompStr;
		_tcscpy(lpConvStr,lpPreResultStr);
		MakeResultString(hIMC,TRUE);
		ImmUnlockIMCC(lpIMC->hCompStr);
		ImmUnlockIMC(hIMC);
		return TRUE;
	}

	ImmUnlockIMCC(lpIMC->hCompStr);
	ImmUnlockIMC(hIMC);
	return FALSE;
}

BOOL GeneratePunct(HIMC hIMC, WORD wCode)
{
	LPINPUTCONTEXT lpIMC;
	LPCOMPOSITIONSTRING lpCompStr;
	LPTSTR lpStr,lpConvStr,lpPreResultStr;
	WORD wHead;
	TCHAR cLastPreResultChar;

	lpIMC = ImmLockIMC(hIMC);
	lpCompStr = (LPCOMPOSITIONSTRING)ImmLockIMCC(lpIMC->hCompStr);
	lpCompStr->dwCompStrLen = 0;
	lpConvStr = ((LPMYCOMPSTR)lpCompStr)->FreePYComp.szConvCompStr;

	if(wConversionSet & CONVERSION_SET_PUNCT) 
	{
		wHead = wCode - _T('!');
		lpStr = aPunct[wHead][0];
		if( _tcslen(lpStr) )
		{
			if( wCode == _T('.') || wCode == _T('/') ) 
			{
				lpPreResultStr = ((LPMYCOMPSTR)lpCompStr)->FreePYComp.szPreResultStr;
				cLastPreResultChar = *CharPrev(lpPreResultStr,lpPreResultStr + _tcslen(lpPreResultStr));
				if(cLastPreResultChar >= _T('!') && cLastPreResultChar <= _T('~') ) 
				{
					*lpConvStr = (TCHAR)wCode;
					*(lpConvStr+1) = _T('\0');
				}
				else
				{
					_tcscpy(lpConvStr,lpStr);
				}
			}
			else 
			{
				_tcscpy(lpConvStr,lpStr);
			}

			MakeResultString(hIMC,TRUE);
			ImmUnlockIMCC(lpIMC->hCompStr);
			ImmUnlockIMC(hIMC);
			return TRUE;
		}
	}
	else 
	{
		*lpConvStr = (TCHAR)wCode;
		*(lpConvStr+1) = _T('\0');
		MakeResultString(hIMC,TRUE);
		ImmUnlockIMCC(lpIMC->hCompStr);
		ImmUnlockIMC(hIMC);
		return TRUE;
	}

	ImmUnlockIMCC(lpIMC->hCompStr);
	ImmUnlockIMC(hIMC);
	return FALSE;
}